package com.hnkywl.sts.action.report;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.report.EjzhmxDao;
import com.hnkywl.sts.entity.organ.Ejzh;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.service.report.EjzhmxService;
import com.hnkywl.sts.service.report.EjzhrjService;
import com.hnkywl.sts.service.sfgl.SkshService;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.Region;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.codehaus.jettison.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 14-1-17
 * Time: 上午9:43
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/report/ejzhrj")
public class EjzhrjAction extends SimpleActionSupport {
    private EjzhrjService ejzhrjService;
    private Long gsId;
    private Long khId;
    private Date endDate;
    private Ejzh ejzh;
    private int page;
    private int rows;
    private Date ejzhrjrq;
    private String gsmc;
    private GsjgDao gsjgDao;
    private Date ejzhscrq;
    private EjzhmxDao ejzhmxDao;

    @Action("ejzhrj")
    public String ejzhrj(){
        endDate = new Date();
        return SUCCESS;
    }

    @Action("to_ejzhrj_rj")
    public String toEjzhrjRj(){
        Gsjg g = gsjgDao.get(gsId);
        gsmc = g.getQc();
        return SUCCESS;
    }

    @Action("to_ejzhrj_delete")
    public String toEjzhrjDelete(){
        Gsjg g = gsjgDao.get(gsId);
        gsmc = g.getQc();
        return SUCCESS;
    }

    @Action("ejzhrjList")
    public void ejzhrjList(){
        renderJson(ejzhrjService.findEjzhrj(gsId, khId, endDate, page, rows));
    }

    @Action("ejzhrj_rj")
    public void rjEjzhrj() {
        try {
            this.ejzhrjService.rjEjzhrj(gsId,ejzhrjrq);
            renderText("{\"statusCode\":\"200\", \"message\":\"二级账户日结成功！\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("ejzhrj_delete")
    public void ejzhrjDelete() {
        try {
            this.ejzhrjService.deleteEjzhrj(gsId,ejzhscrq);
            renderText("{\"statusCode\":\"200\", \"message\":\"二级账户删除日结成功！\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    /**
     * 二级账户日结导出
     *
     * @return
     */
    @Action("ejzhrjExport")
    public String ejzhrjExport() {
        java.text.DecimalFormat df=new java.text.DecimalFormat("#,###.00");
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(endDate);
        c.add(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Map result = ejzhmxDao.zjrjList(gsId, khId, endDate,c.getTime(), 1, -1);
        List<Map<String,Object>> ejzhrjList = (List<Map<String,Object>>)result.get("rows");
        //第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("二级账户日结");
        POIUtil poiUtil = new POIUtil(wb);
        //设置列宽
        sheet.setDefaultRowHeightInPoints(15);
        sheet.setDefaultColumnWidth((short) 7);
        sheet.setColumnWidth((short) 0, (short) (11 * 512));
        sheet.setColumnWidth((short) 1, (short) (9 * 512));
        sheet.setColumnWidth((short) 2, (short) (9 * 512));
        sheet.setColumnWidth((short) 3, (short) (15 * 512));
        sheet.setColumnWidth((short) 4, (short) (15 * 512));
        sheet.setColumnWidth((short) 5, (short) (6 * 512));
        sheet.setColumnWidth((short) 6, (short) (6 * 512));
        sheet.setColumnWidth((short) 7, (short) (10 * 512));
        //第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        row.setHeightInPoints(36);
        //第四步，创建单元格，并设置值表头  设置表头居中
        HSSFCellStyle titleStyle = poiUtil.createTitleHSSFCellStyle(wb);
        HSSFCell titleCell = row.createCell((short) 0);
        titleCell.setCellStyle(titleStyle);
        titleCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        titleCell.setCellValue("二级账户日结汇总");
        // 四个参数分别是：起始行，起始列，结束行，结束列
        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 7));
        row = sheet.createRow((int) 1);
        row.setHeightInPoints(21);
        //生成第一行数据
        HSSFCell data = row.createCell((short) 0);
        data.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        data.setEncoding(HSSFCell.ENCODING_UTF_16);
        data.setCellValue("年|月|日");

        HSSFCell yq = row.createCell((short) 1);
        yq.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        yq.setEncoding(HSSFCell.ENCODING_UTF_16);
        yq.setCellValue("园区");

        HSSFCell khmc = row.createCell((short) 2);
        khmc.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        khmc.setEncoding(HSSFCell.ENCODING_UTF_16);
        khmc.setCellValue("客户名称");

        HSSFCell ejzhmc = row.createCell((short) 3);
        ejzhmc.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        ejzhmc.setEncoding(HSSFCell.ENCODING_UTF_16);
        ejzhmc.setCellValue("二级账户名称");

        HSSFCell ejzh = row.createCell((short) 4);
        ejzh.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        ejzh.setEncoding(HSSFCell.ENCODING_UTF_16);
        ejzh.setCellValue("二级账户");

        HSSFCell zhye = row.createCell((short) 5);
        zhye.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        zhye.setEncoding(HSSFCell.ENCODING_UTF_16);
        zhye.setCellValue("账户余额");

        HSSFCell dfje = row.createCell((short) 6);
        dfje.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        dfje.setEncoding(HSSFCell.ENCODING_UTF_16);
        dfje.setCellValue("贷方金额");

        HSSFCell jfje = row.createCell((short) 7);
        jfje.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        jfje.setEncoding(HSSFCell.ENCODING_UTF_16);
        jfje.setCellValue("借方金额");
        //表体部分
        for (int i = 2; i < ejzhrjList.size() + 2; i++) {
            row = sheet.createRow(i);
            row.setHeightInPoints(19);

            data = row.createCell((short) 0);
            data.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            data.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != ejzhrjList.get(i - 2).get("yearDate")) {
                data.setCellValue(ejzhrjList.get(i - 2).get("yearDate").toString());
            }

            yq = row.createCell((short) 1);
            yq.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            yq.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != ejzhrjList.get(i - 2).get("gsmc")) {
                yq.setCellValue(ejzhrjList.get(i - 2).get("gsmc").toString());
            }

            khmc = row.createCell((short) 2);
            khmc.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            khmc.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != ejzhrjList.get(i - 2).get("khmc")) {
                khmc.setCellValue(ejzhrjList.get(i - 2).get("khmc").toString());
            }

            ejzhmc = row.createCell((short) 3);
            ejzhmc.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            ejzhmc.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != ejzhrjList.get(i - 2).get("ejzhmc")) {
                ejzhmc.setCellValue(ejzhrjList.get(i - 2).get("ejzhmc").toString());
            }

            ejzh = row.createCell((short) 4);
            ejzh.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            ejzh.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != ejzhrjList.get(i - 2).get("zh")) {
                ejzh.setCellValue(ejzhrjList.get(i - 2).get("zh").toString());
            }

            zhye = row.createCell((short) 5);
            zhye.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            zhye.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != ejzhrjList.get(i - 2).get("zhye")) {
                String ye =
                        df.format(new BigDecimal(Double.parseDouble(ejzhrjList.get(i - 2).get("zhye").toString())));
                zhye.setCellValue(ye);
            }

            dfje = row.createCell((short) 6);
            dfje.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            dfje.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != ejzhrjList.get(i - 2).get("dfje")) {
                String dfje1 =
                        df.format(new BigDecimal(Double.parseDouble(ejzhrjList.get(i - 2).get("dfje").toString())));
                dfje.setCellValue(dfje1);
            }

            jfje = row.createCell((short) 7);
            jfje.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            jfje.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != ejzhrjList.get(i - 2).get("jfje")) {
                String jfje1 =
                        df.format(new BigDecimal(Double.parseDouble(ejzhrjList.get(i - 2).get("jfje").toString())));
                jfje.setCellValue(jfje1);
            }

            }
            try {
                response.setContentType("application/x-download");
                String filedisplay = new String(("二级账户日结导出.xls").getBytes("gb2312"), "iso8859-1");
                response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
                wb.write(response.getOutputStream());
                response.getOutputStream().close();
            } catch (Exception e) {
                e.printStackTrace();
                renderText("位置异常，导出失败" + e.getMessage());
            }
            return null;
        }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Long getKhId() {
        return khId;
    }

    public void setKhId(Long khId) {
        this.khId = khId;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Ejzh getEjzh() {
        return ejzh;
    }

    public void setEjzh(Ejzh ejzh) {
        this.ejzh = ejzh;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Date getEjzhrjrq() {
        return ejzhrjrq;
    }

    public void setEjzhrjrq(Date ejzhrjrq) {
        this.ejzhrjrq = ejzhrjrq;
    }


    public String getGsmc() {
        return gsmc;
    }

    public void setGsmc(String gsmc) {
        this.gsmc = gsmc;
    }

    public Date getEjzhscrq() {
        return ejzhscrq;
    }

    public void setEjzhscrq(Date ejzhscrq) {
        this.ejzhscrq = ejzhscrq;
    }

    @Autowired
    public void setEjzhrjService(EjzhrjService ejzhrjService) {
        this.ejzhrjService = ejzhrjService;
    }
    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }
    @Autowired
    public void setEjzhmxDao(EjzhmxDao ejzhmxDao) {
        this.ejzhmxDao = ejzhmxDao;
    }
}
